//
//  ChangePhoneViewController.m
//  YunGou
//
//  Created by macbook on 16/5/30.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "ChangePhoneViewController.h"
#import "UIBarButtonItem+Extension.h"
#import "NSString+Regexp.h"

@interface ChangePhoneViewController ()
@property (weak, nonatomic) IBOutlet UILabel *nowMobileLabel;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UITextField *NewMobile;
@property (weak, nonatomic) IBOutlet UITextField *code;
@property (weak, nonatomic) IBOutlet UIButton *gainCodeBtn;

@property (assign,nonatomic) long content;
@property (nonatomic,copy) NSString *contentText;
@property (nonatomic,strong) NSDate *startDate;
@property (strong,nonatomic) NSTimer *timer;
@end

@implementation ChangePhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *rightBarItem = [UIBarButtonItem barButtonTitle:@"确认" image:nil frame:CGRectMake(0, 0, 30, 44) target:self sel:@selector(OkClicked)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    
    NSMutableString *mobile = [NSMutableString stringWithString:[UserModel sharedInstance].mobile];
    [mobile replaceCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
    _nowMobileLabel.text = [NSString stringWithFormat:@"当前手机号：%@",mobile];
}

- (void)OkClicked{
    if (_password.text.length == 0) {
        [MBProgressHUD showError:@"请输入密码"];
    }else if (_NewMobile.text.length == 0){
        [MBProgressHUD showError:@"请输入号码"];
    }else if (![NSString isMobileNumber:_NewMobile.text]){
        [MBProgressHUD showError:@"号码格式不正确"];
    }else if (![[NSString stringWithFormat:@"%ld",self.content] isEqualToString:_code.text]){
        [MBProgressHUD showError:@"验证码输入不正确"];
    }else{
        [NetWorkTool changeMobileWithOldMobile:[UserModel sharedInstance].mobile newMobile:_NewMobile.text password:_password.text success:^(id object) {
            NSLog(@"%@",object);
        } failure:^(NSError *error) {
            
        }];
    }
}

- (IBAction)gainCodeBtnClicked:(id)sender {
    if (_NewMobile.text.length == 0) {
        [MBProgressHUD showError:@"号码不能为空"];
    }else{
        BOOL isPhoneNum = [NSString isMobileNumber:self.NewMobile.text];
        if (isPhoneNum) {
            self.gainCodeBtn.enabled = NO;
            self.content = (arc4random()%1000000) + 100000;
            self.contentText = [NSString stringWithFormat:@"【PK夺宝】您的注册验证码为:%ld",self.content];
            NSLog(@"%ld",self.content);
            //            [NetWorkTool sendVerfiWithPhone:self.telText.text content:self.contentText success:^(id object) {
            //                [MBProgressHUD showSuccess:@"发送成功"];
            //            } failure:^(NSError *error) {
            //                [MBProgressHUD showError:@"发送失败"];
            //            }];
            //定时器
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
            _startDate = [NSDate date];
            [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
        }
    }
}

//辅助函数
- (void)updateTime:(NSTimer*)sender
{
    double deltaTime = [[NSDate date] timeIntervalSinceDate:_startDate];
    int second = 60 - (int)(deltaTime + 0.5);
    if (second < 0.0) {
        [self.timer setFireDate:[NSDate distantFuture]];
        self.gainCodeBtn.enabled = YES;
        [self.gainCodeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        [self.gainCodeBtn setTitle:@"重新获取" forState:UIControlStateDisabled];
    }else{
        NSString *title = [NSString stringWithFormat:@"%d秒",second];
        [self.gainCodeBtn setTitle:title forState:UIControlStateNormal];
        [self.gainCodeBtn setTitle:title forState:UIControlStateDisabled];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
